--[[

	GModRP - Real Garry's Mod Roleplay.
			By Slob187.
		

]]


function GM:InitPostEntity()

	timer.Simple( 3, function()
	
		print( "GModRP: Loading unownable doors..." )

		local rDoorsF = "GModRP/doors/"
		local curMap = game.GetMap()
		local unOwnableDoors = {}


		if !file.Exists( rDoorsF.."rp_oviscity_gmc4.txt" ) then

			local ovisDoors = { "-2624.8301 -1080.0000 -331.0000", "-2490.0000 -2068.0000 -330.0000",
			"-1767.0472 -1910.9688 -327.2162", "-468.0313 -1535.8856 -337.0611", "-459.4050 -535.0000 -330.0000",
			"-6696.0000 191.5000 -808.0000", "-420.1800 2420.7000 -330.2810", "-1140.0900 2363.2800 -330.2810",
			"-4474.0000 1152.0000 -328.0000", "-6632.0396 594.0180 -808.0000", "-6632.0396 427.9820 -808.0000" }
			
			file.Write( rDoorsF.."rp_oviscity_gmc4.txt", util.TableToKeyValues( ovisDoors ) )
			
		end


		if file.Exists( rDoorsF..curMap..".txt" ) then

			for k, v in pairs( util.KeyValuesToTable( file.Read( rDoorsF..curMap..".txt" ) ) ) do
			
				local vecExpl = string.Explode( " ", v )
				local toVector = Vector( tonumber( vecExpl[1] ), tonumber( vecExpl[2] ), tonumber( vecExpl[3] ) )
				
				unOwnableDoors[k] = toVector
				
			end	
			
			for _, vec in pairs( unOwnableDoors ) do
			
				for _, v in pairs( ents.FindInSphere( vec, 50 ) ) do
				
					if v:isOwnable() then
					
						v:SetNWString( "doorowner", "UNOWNABLE" )
						
					end

				end
				
			end
			
		end
		
		print( "GModRP: Unownable doors loaded." )
		
	end )	
	
end


GMRP.command( "door_getpos", function( p )

	if GMRP.UseLimit( p ) then
	
		local traceEnt = p:GetEyeTrace().Entity

		if traceEnt:isOwnable() then

			p:PrintMessage( HUD_PRINTCONSOLE, tostring( traceEnt:GetPos() ) )

		else

			p:ChatPrint( "Trace entity is not door / vehicle!" )
			
		end	
		
	end
	
end )	